<template>
<div class="wrapper">
  <head title="SecKill"></head>
  <scroller class="titleScro" scroll-direction="horizontal">
    <div
      class="item"
      v-for="(item,index) in timeArr"
      :key="index"
      @click="activeType='';activeTime=item.value;page=1;changeList(index)"
    >
      <text class="timeItem" :class="[activeTime==item.value?'active':'']">{{item.time}}</text>
      <text class="titleItem" :class="[activeTime==item.value?'active':'']">{{item.title}}</text>
    </div>
  </scroller>
  <!-- <div class="curAct">
    <text class="curTit">Start in the first place the order first</text>
    <div class="curCon">
      <text class="endTime">End</text>
      <text class="timeNum">03</text>
      <text class="timesyb">:</text>
      <text class="timeNum">03</text>
      <text class="timesyb">:</text>
      <text class="timeNum">11</text>
    </div>
  </div>-->
  <!-- <div class="curActC" v-if="shopType.length>0">
    <scroller class="shopType" scroll-direction="horizontal">
      <text
        class="typeItem"
        :class="[!activeType?'typeActive':'']"
        @click="activeType='';page=1;changeList()"
      >Recommended</text>
      <text
        class="typeItem"
        :class="[activeType==item.id?'typeActive':'']"
        v-for="(item,index) in shopType"
        :key="index"
        @click="activeType=item.id;page=1;changeList()"
      >{{item.name}}</text>
    </scroller>
    <text class="iconfont search" @click="push('root:app/active/activeSearch.js',{type:2})">&#xe62a;</text>
  </div>-->
  <scroller class="shopScro">
    <div class="shopItem" v-for="(item,index) in goodsList" :key="index">
      <image
        class="shopImg"
        :src="item.thumb"
        resize="cover"
        @click="push('root:app/active/secKillDetail.js',{id:item.id})"
      />
      <div class="shopCon">
        <div class="shopTop">
          <text class="shopTit">{{item.title}}</text>
          <!-- <text class="titBorder">2 pieces of eight fold</text> -->
        </div>
        <div class="shopBot">
          <div class="priceInfo">
            <div class="priceBx">
              <text class="symbol">AU$</text>
              <text class="cuPrice">{{item.price}}</text>
            </div>
            <text class="oldPrice" v-if="item.old_price-0>0">AU$ {{item.old_price}}</text>
          </div>

          <text
            class="payBtn"
            :class="[new Date().getTime()>new Date(item.start_time.replace(/-/g, '/')).getTime()?'':'grayBtn']"
            @click="buyShop(item)"
          >Buy</text>
        </div>
      </div>
    </div>
    <text class="loadingTxt" v-if="isLoading">Please wait a minute...</text>
    <empty v-if="goodsList.length==0&&!isLoading" tips="No results found" img="root:img/e_seel.png"></empty>
    <div class="mt20"></div>
  </scroller>
  <div class="maskClack" @click="e=>e.stopPropagation()" v-if="isLoading">
    <floading class="indicator" color="#303030"></floading>
  </div>
</div>
</template>

<script>
const navigator = weex.requireModule("navigator");

import asCore from "../../mixin/core";

import { getActiveList, get_category_list } from "../../mixin/ajax";
export default {
  data() {
    return {
      page: 1,
      passwd: "",
      activeTime: "1",
      shopType: [],
      activeType: "",
      endTime: "",
      timeArr: [
        { time: "00:00", title: "soon", value: "7" },
        { time: "9:00", title: "Tomorrow", value: "19" },
        { time: "10:00", title: "Tomorrow", value: "20" },
        { time: "11:00", title: "Tomorrow", value: "21" },
        { time: "12:00", title: "Tomorrow", value: "23" },
        { time: "13:00", title: "Tomorrow", value: "24" },
        { time: "14:00", title: "Tomorrow", value: "25" },
        { time: "15:00", title: "Tomorrow", value: "26" },
        { time: "16:00", title: "Tomorrow", value: "26" },
        { time: "17:00", title: "Tomorrow", value: "26" },
        { time: "18:00", title: "Tomorrow", value: "26" },
        { time: "19:00", title: "Tomorrow", value: "27" },
        { time: "20:00", title: "Tomorrow", value: "27" },
        { time: "22:00", title: "Tomorrow", value: "27" },
        { time: "23:00", title: "Tomorrow", value: "27" }
      ],
      goodsList: [],
      userId: "",
      loginType: "email"
    };
  },

  created() {
    asCore.getBsessionid(sessionid => {
      this.userId = sessionid;
    });
    let cutTime = new Date();
    let hh = cutTime.getHours();
    let yy = cutTime.getFullYear(),
      mm = cutTime.getMonth() + 1,
      dd = cutTime.getDate();
    this.activeTime = yy + "-" + mm + "-" + dd + " " + hh + ":00";

    let arr = [];
    for (let i = hh; i < 24; i++) {
      arr[arr.length] = {
        time: i + ":00",
        title: i == hh ? "start" : "soon",
        value: yy + "-" + mm + "-" + dd + " " + i + ":00"
      };
    }
    var day3 = new Date();

    day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
    let yys = day3.getFullYear(),
      mms = day3.getMonth() + 1,
      dds = day3.getDate();
    let tommornArr = [];
    for (let i = 9; i < 24; i++) {
      tommornArr[tommornArr.length] = {
        time: i + ":00",
        title: i == hh ? "start" : "soon",
        value: yys + "-" + mms + "-" + dds + " " + i + ":00"
      };
    }
    this.timeArr = arr.concat(tommornArr);

    var pref = weex.requireModule("storage");
    pref.getItem("categroy1", event => {
      if (event.result == "success") {
        this.shopType = JSON.parse(event.data);
      } else {
        this.getPageData();
      }
    });

    this.changeList(0);
  },
  methods: {
    getPageData() {
      
      get_category_list({ type: 1, parentid: 0 }, (res, flag) => {
       
        if (flag) {
          if (res.code == 200) {
            this.shopType = res.data;
          }
        }
      });
    },
    changeList(index) {
      this.page = 1;
      this.endTime = this.timeArr[index + 1].value;
      this.getList();
    },
    gonext(url, parmar) {
      if (!this.userId) {
        this.push("root:app/login/login.js");
      } else {
        this.push(url, parmar);
      }
    },
    buyShop(item) {
      let noeDate = new Date().getTime();
      let startDate = new Date(item.start_time.replace(/-/g, "/")).getTime();
      let endtDate = new Date(item.end_time.replace(/-/g, "/")).getTime();
      if (noeDate < startDate) {
        this.alert("Please wait, the event is about to start.");
        return false;
      } else if (noeDate > endtDate) {
        this.alert("Sorry, the event has finished.");
        return false;
      }
      this.gonext("root:app/order/confirmation.js", {
        id: item.id,
        activity: true
      });
    },
    getList() {
      
      if (this.page == 1) {
        this.goodsList = [];
      }
      this.isLoading = true;
      getActiveList(
        {
          users_id: "",
          start_time: this.activeTime,
          next_start_time: this.endTime,
          keywords: "",
          categoryid: this.activeType,
          type: "1",
          page: this.page,
          page_num: 10
        },
        (res, flag) => {
          this.isLoading = false;
         
          if (flag) {
            if (res.code == "200") {
              if (this.page == 1) {
                this.goodsList = res.data.list;
              } else {
                this.goodsList = this.goodsList.concat(res.data.list);
              }
            }
          }
        }
      );
    },
    detail() {
      this.push("root:app/active/secKillDetail.js");
    }
  }
};
</script>

<style scoped>
.titleScro {
  width: 750px;
  height: 120px;
  align-items: center;
  background-color: #303030;
  flex-direction: row;
  justify-content: flex-start;
}
.iconfont {
  font-family: iconfont;
}
.wrapper {
  background-color: #f5f5f5;
}
.item {
  width: 140px;
  height: 120px;
  justify-content: center;
  align-items: center;
}
.timeItem {
  font-size: 38px;
  height: 53px;
  line-height: 53px;
  color: #666;
}
.titleItem {
  font-size: 28px;
  height: 40px;
  line-height: 40px;
  color: #666;
}

.maskClack {
  position: fixed;
  left: 0;
  bottom: 0;
  right: 0;
  top: 0;
  justify-content: center;
  align-items: center;
  background-color: transparent;
}
.indicator {
  width: 40px;
  height: 40px;
}
.active {
  color: #ffffff;
}
.loadingTxt {
  text-align: center;
  padding: 50px 0;
  font-size: 28px;
  color: #999999;
}
.curAct {
  flex-direction: row;
  justify-content: space-between;
  width: 750px;
  align-items: center;
  height: 88px;
  padding: 0 32px;
  background-color: #fff;
}
.curActC {
  flex-direction: row;
  justify-content: space-between;
  width: 750px;
  align-items: center;
  height: 88px;
  padding-left: 32px;
  background-color: #fff;
}
.mt20 {
  margin-top: 20px;
}
.curTit {
  color: #333;
  font-size: 22px;
}
.curCon {
  flex-direction: row;
  align-items: center;
}
.endTime {
  margin-right: 20px;
}
.timeNum {
  width: 30px;
  height: 30px;
  background-color: #bb8732;
  color: #fff;
  border-radius: 5px;
  text-align: center;
  line-height: 30px;
  font-size: 20px;
}

.timesyb {
  color: #bb8732;
  margin: 0 2px;
}

.shopType {
  flex: 1;
}
.search {
  width: 88px;
  text-align: center;
  line-height: 88px;
  font-size: 40px;
  color: #333;
}
.shopType {
  flex-direction: row;
  align-items: center;
}
.typeItem {
  height: 88px;
  line-height: 88px;
  font-size: 30px;
  margin: 0 20px;
  color: #505050;
}
.typeActive {
  border-bottom-color: #ba8833;
  border-bottom-style: solid;
  border-bottom-width: 4px;
}
.shopScro {
  width: 750px;
  flex: 1;
  align-items: center;
  flex-direction: column;
  justify-content: flex-start;
}
.shopItem {
  width: 686px;
  height: 268px;
  background-color: rgba(255, 255, 255, 1);
  border-radius: 16px;
  margin-top: 20px;
  padding: 24px;
  flex-direction: row;
}
.shopCon {
  flex: 1;
  height: 220px;
  align-items: flex-start;
  justify-content: space-between;
}
.shopTop {
  align-items: flex-start;
}
.shopImg {
  width: 220px;
  height: 220px;
  margin-right: 24px;
  background-color: #f2f2f2;
  border-radius: 10px;
}
.shopTit {
  lines: 1;
  text-overflow: ellipsis;
  font-size: 22px;
  color: #1e1e1e;
}
.titBorder {
  height: 46px;
  border-radius: 23px;
  border-width: 1px;
  border-style: solid;
  border-color: rgba(196, 133, 21, 1);
  line-height: 46px;
  padding: 0 23px;
  font-size: 22px;
  color: #1e1e1e;
  margin-top: 15px;
}
.shopBot {
  width: 390px;
  height: 80px;
  justify-content: space-between;
  flex-direction: row;
  align-items: center;
}
.priceInfo {
  align-items: flex-start;
}
.priceBx {
  flex-direction: row;
  align-items: center;
  height: 45px;
}
.symbol {
  font-size: 20px;
  color: #e52424;
  margin-right: 5px;
}
.cuPrice {
  color: #e52424;
  font-size: 32px;
}
.oldPrice {
  font-size: 22px;
  color: #999999;
  margin-top: 5px;
  text-decoration: line-through;
}
.payBtn {
  height: 50px;
  line-height: 50px;
  background-color: #ec414d;
  color: #fff;
  border-radius: 25px;
  text-align: center;
  width: 130px;
  font-size: 26px;
}
.grayBtn {
  background-color: #666;
}
</style>
